package com.qqt.csr.archive.batch.task.etl.writer;

import com.qqt.csr.archive.batch.task.context.MessageArchiveTaskContext;
import com.qqt.csr.archive.entity.writer.ArchiveMessage;
import com.qqt.csr.archive.service.ArchiveMessageService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: yang hao
 * @Date: 2024/11/20 10:20
 * @Description: 消息归档 写入器
 **/
@Slf4j
@Component
@SuppressWarnings("unused")
public class MessageArchiveWriter implements ItemWriter<List<ArchiveMessage>> {

    @Autowired
    private ArchiveMessageService archiveMessageService;

    /**
     * 写入归档消息
     * @param chunk {@link MessageArchiveTaskContext} 消息归档上下文
     */
    @Override
    public void write(@NonNull Chunk<? extends List<ArchiveMessage>> chunk) {
        List<ArchiveMessage> messages = chunk.getItems().stream().flatMap(List::stream).toList();
        log.debug("写入归档消息, 数量:{}", messages.size());
        archiveMessageService.save(messages);
    }
}
